package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.interfaces.PermissionService;
import cn.itcast.pojo.*;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/8/23 9:02
 * @description: TODO
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("add permission error.",e);
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 分页查询权限列表
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = permissionService.pageQuery(
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),
                    queryPageBean.getQueryString()
            );
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS,pageResult);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("findPage checkitem error.",e);
            return new Result(false,MessageConstant.GET_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id获取权限的信息
     * @param id
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/findPermissionById")
    public Result findCheckItemById(Integer id){
        try {
            Permission permission = permissionService.findPermissionById(id);
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Query permission By Id error.",e);
            return new Result(false,MessageConstant.GET_PERMISSION_FAIL);
        }
    }

    /**
     * 提交编辑后的权限
     * @param permission
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Edit permission error.",e);
            return new Result(true,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /***
     * 删除对应id的权限
     * @param id
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
        } catch (RuntimeException r){
            return new Result(false,r.getMessage());
        } catch (Exception e) {
            //e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    /**
     * 获取所有的权限
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> permissions =  permissionService.findAll();
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,permissions);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Query All permission error.",e);
            return new Result(false,MessageConstant.GET_PERMISSION_SUCCESS);
        }
    }


}
